Mix ActiveModel::AttributeMethods into ActiveResource::Base#465
Open
seanpdoyle wants to merge 1 commit intorails:mainfrom
Open
Mix ActiveModel::AttributeMethods into ActiveResource::Base#465seanpdoyle wants to merge 1 commit intorails:mainfrom
ActiveModel::AttributeMethods into ActiveResource::Base#465seanpdoyle wants to merge 1 commit intorails:mainfrom
Conversation
Member
|
Do you have benchmarks to show if this change changed the performance of reading and writing attributes? |
Contributor
Author
|
@rafaelfranca after making the following benchmark-related changes: diff --git a/lib/active_resource/base.rb b/lib/active_resource/base.rb
index e34714e..f43675d 100644
--- a/lib/active_resource/base.rb
+++ b/lib/active_resource/base.rb
@@ -371,6 +371,7 @@ module ActiveResource
@@logger = logger
end
+ class_attribute :define_methods, default: true
class_attribute :lazy_collections, default: true, instance_accessor: false
class_attribute :_query_format
class_attribute :_format
@@ -463,7 +464,7 @@ module ActiveResource
@schema[k] = v
@known_attributes << k
end
- define_attribute_methods @known_attributes
+ define_attribute_methods(@known_attributes) if define_methods
@schema
else
@@ -493,7 +494,7 @@ module ActiveResource
# purposefully nulling out the schema
@schema = nil
@known_attributes = []
- undefine_attribute_methods
+ undefine_attribute_methods if define_methods
return
endI executed the following script: # frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activeresource", path: "."
gem "benchmark-ips"
end
class WithAttributeMethods < ActiveResource::Base
self.define_methods = true
schema do
attribute :known, :string
end
end
class WithoutAttributeMethods < ActiveResource::Base
self.define_methods = false
schema do
attribute :known, :string
end
end
require "benchmark/ips"
Benchmark.ips do |x|
x.report("with") do |n|
with = WithAttributeMethods.new
n.times do
with.known = "value"
with.known
with.unknown = "value"
with.unknown
end
end
x.report("without") do |n|
without = WithoutAttributeMethods.new
n.times do
without.known = "value"
without.known
without.unknown = "value"
without.unknown
end
end
x.compare!
endThe While improving performance (or at least not hindering performance) is one motivation, this change is also required for the |
0d99c0e to
01af166
Compare
The problem --- The current attribute reading and writing implementation relies upon `#method_missing` and `#respond_to_missing?` for access. When attributes are defined by a schema, it's possible to define methods for them at "declaration time", rather than implementing access entirely through method missing reflection. The solution --- Mix [ActiveModel::AttributeMethods][] into `ActiveResource::Base`, then hook into `schema` assignment. When assigning attributes, invoke [define_attribute_methods][]. When the schema is reset to be `nil`, invoke [undefine_attribute_methods][]. [ActiveModel::AttributeMethods]: https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html [define_attribute_methods]: https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods/ClassMethods.html#method-i-define_attribute_methods [undefine_attribute_methods]: https://edgeapi.rubyonrails.org/classes/ActiveModel/AttributeMethods/ClassMethods.html#method-i-undefine_attribute_methods
01af166 to
26f06e0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
The current attribute reading and writing implementation relies upon
#method_missingand#respond_to_missing?for access.When attributes are defined by a schema, it's possible to define methods for them at "declaration time", rather than implementing access entirely through method missing reflection.
The solution
Mix ActiveModel::AttributeMethods into
ActiveResource::Base, then hook intoschemaassignment. When assigning attributes, invoke define_attribute_methods. When the schema is reset to benil, invoke undefine_attribute_methods.